<!DOCTYPE html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('权限列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form action="" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">关键字</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="powerName" placeholder="" type="text">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-filter="power-query" lay-submit>
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button class="pear-btn pear-btn-md" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="power-table" lay-filter="power-table"></table>
    </div>
</div>
<script id="power-toolbar" type="text/html">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">新增</button>
</script>
<script id="icon" type="text/html">
    <i class="layui-icon {{d.icon}}"></i>
</script>
<script id="power-type" type="text/html">
{{#if (d.menuType == '0'){ }}
    <span>目录</span>
{{# }else if(d.menuType == 1){ }}
    <span>菜单</span>
{{# } else if(d.menuType == 2){ }}
    <span>按钮</span>
{{# } }}
</script>
<script id="power-show" type="text/html">
    <input type="checkbox" name="show" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏"
        lay-filter="menu-show" {{ d.isShow=='1'?'checked':'' }}/>
</script>
<script id="power-bar" type="text/html">
    {{# if(d.menuType < 2){ }}
    <button
            class="pear-btn pear-btn-primary pear-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>
    </button>
    {{# } }}
    <button
            class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>

    <button sec:authorize="hasPermission(#vars.ActionGUID,'remove')"
            class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>
</body>
<th:block th:include="include :: footer"/>
<script type="text/javascript">
    let listUrl = '[(${@htmlHelper.actionGuid("list")})]';
    let addUrl = '[(${@htmlHelper.actionGuid("add")})]';
    let editUrl = '[(${@htmlHelper.actionGuid("edit")})]';
    let clearCacheUrl = '[(${@htmlHelper.actionGuid("clearCache")})]';
    let actionEnableUrl = '[(${@htmlHelper.actionGuid("enable")})]';
    let leftTreeUrl = '[(${@htmlHelper.actionGuid("tree")})]';
</script>
<script>
    layui.use(['table', 'form', 'jquery', 'treetable', 'popup'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        let popup = layui.popup;

        treetable.render({
            treeColIndex:0,
            //treeSpid 树形结构起始父类的id
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'parentId',
            skin:'line',
            method: "POST",
            toolbar: '#power-toolbar',
            elem: '#power-table',
            //url: contextPath + '/system/power/data',
            url:createLink(listUrl),
            page: false,
            //parseData:true,
            tree:{
                view:{
                    //indent:10,
                    expandAllDefault:false //是否默认展开全部节点 默认就是false 没效果
                }
            },
            defaultToolbar: [{
                //这里可以自定义按钮
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
                title: '刷新'
            }, 'filter', 'print', 'exports'//筛选，打印，导出
            ],
            cols: [
                [
                    {field: 'title', title: '权限名称'},
                    {field:'icon',title:'图标',templet:'#icon'},
                    {field: 'menuType',title:'权限类型',templet:"#power-type"},
                    {field: 'isShow',title: "是否展示",templet: "#power-show"},
                    {field: 'sort',title: '排序'},
                    {title: '操作', templet: '#power-bar', width: 150, align: 'center'}
                ]
            ]
        });

        let MODULE_PATH = contextPath + "/system/power/";

        //监控工具栏的事件， toolbar就表示是工具栏了
        //参考： https://layui.org.cn/demo/table/toolbar.html
        table.on('toolbar(power-table)',function (obj) {
            if(obj.event === 'add'){
                window.add();
            }else if(obj.event === 'refresh'){
                window.refresh();
            }
        });

        //监控数据行的事件， tool就表示是数据行
        //参考： https://layui.org.cn/demo/table/toolbar.html
        table.on('tool(power-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }else if (obj.event === 'add') {
                window.add(obj.data["id"]);
            }
        });

        //顶部查询提交
        form.on('submit(power-query)', function (data) {
            treetable.search("#power-table", data.field.powerName)
            return false;
        });

        form.on('switch(menu-show)', function (obj) {
            let operate;
            if (obj.elem.checked) {
                operate = "show";
            } else {
                operate = "hide";
            }
            let loading = layer.load();
            $.ajax({
                url: MODULE_PATH + operate,
                data: JSON.stringify({id: this.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.success) {
                        popup.success(result.msg);
                    } else {
                        popup.failure(result.msg);
                    }
                }
            })
        });

        window.add = function (parentId) {
            if(parentId==null)parentId='';
            layer.open({
                type:2,
                title:'新增',
                shade:0.1,
                area:['450px','500px'],
                //content:MODULE_PATH + 'add?parentId=' + parentId
                content: createLink(addUrl) + '&parentId='+ parentId
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['450px', '500px'],
                //content: MODULE_PATH + 'edit?powerId=' + obj.data['id']
                content: createLink(editUrl) + "&powerId=" + obj.data['id']
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该权限', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del();
                            })
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.refresh = function () {
            treetable.reload('#power-table');
        }



    });
</script>
</html>